#include <memory>
#include <unordered_map>
#include "Thread.hpp"

// 多线程的模板案例
// 使用shared_ptr智能指针管理
#define NUM 5
using thread_ptr_t = std::shared_ptr<ThreadModule::Thread>;

int main()
{
    // 先描述再组织
    std::unordered_map<std::string, thread_ptr_t> threads;
    for (int i = 0; i < NUM; i++)
    {
        thread_ptr_t t = std::make_shared<ThreadModule::Thread>([]() { // 注意 ： make_shared 是 C++17 的
            while (true)
            {
                std::cout << "Hello world" << std::endl;
                sleep(1);
            }
        });
        threads[t->Name()] = t;
    }

    for (auto &thread : threads)
    {
        thread.second->Start();
    }

    for (auto &thread : threads)
    {
        thread.second->Join();
    }

    return 0;
}